package com.sinothk.fc.employee.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinothk.fc.employee.entity.StoreEmployeeEntity;
import com.sinothk.fc.employee.entity.StoreEmployeeVO;
import com.sinothk.fc.employee.service.IStoreEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 门店员工表
 * @Author: Liangyt
 * @Date: 2022-09-04
 * @Version: V1.0
 */
@Api(tags = "门店员工表")
@RestController
@RequestMapping("/fc/storeEmployeeCtrl")
@Slf4j
public class StoreEmployeeController extends JeecgController<StoreEmployeeEntity, IStoreEmployeeService> {
    @Autowired
    private IStoreEmployeeService storeEmployeeService;

    /**
     * 分页列表查询
     *
     * @param storeEmployee
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "门店员工表-分页列表查询")
    @ApiOperation(value = "门店员工表-分页列表查询", notes = "门店员工表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(StoreEmployeeEntity storeEmployee,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<StoreEmployeeEntity> queryWrapper = QueryGenerator.initQueryWrapper(storeEmployee, req.getParameterMap());
        Page<StoreEmployeeEntity> page = new Page<StoreEmployeeEntity>(pageNo, pageSize);
        IPage<StoreEmployeeEntity> pageList = storeEmployeeService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 分页列表-员工列表
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "门店-员工分页列表")
    @ApiOperation(value = "门店-员工分页列表", notes = "门店-员工分页列表")
    @GetMapping(value = "/pageForCurrStore")
    public Result<?> pageForCurrStore( //StoreEmployeeEntity storeEmployee,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        IPage<StoreEmployeeVO> pageList = storeEmployeeService.pageForCurrStore(new Page<>(pageNo, pageSize));
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param storeEmployee
     * @return
     */
    @AutoLog(value = "门店员工表-添加")
    @ApiOperation(value = "门店员工表-添加", notes = "门店员工表-添加")
    @PostMapping(value = "/add")

    public Result<?> add(@RequestBody StoreEmployeeEntity storeEmployee) {
        storeEmployeeService.save(storeEmployee);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param storeEmployee
     * @return
     */
    @AutoLog(value = "门店员工表-编辑")
    @ApiOperation(value = "门店员工表-编辑", notes = "门店员工表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody StoreEmployeeEntity storeEmployee) throws Exception {
        if (StringUtils.isNotBlank(storeEmployee.getId())) {
            storeEmployeeService.updateById(storeEmployee);
        }else{
            storeEmployeeService.updateByStoreIdAndPhone(storeEmployee);
        }
        return Result.OK(storeEmployee);
    }

    /**
     * 绑定门店与员工
     *
     * @param storeId
     * @param employeePhone
     * @return
     */
    @AutoLog(value = "绑定门店与员工")
    @ApiOperation(value = "绑定门店与员工", notes = "绑定门店与员工")
    @GetMapping(value = "/binding")
    public Result<?> binding(@RequestParam(name = "storeId") String storeId, @RequestParam(name = "employeePhone") String employeePhone) throws Exception {
        StoreEmployeeEntity employee = storeEmployeeService.binding(storeId, employeePhone);
        return Result.OK(employee);
    }

    /**
     * 解绑门店与员工
     *
     * @param storeId
     * @param employeePhone
     * @return
     */
    @AutoLog(value = "解绑门店与员工")
    @ApiOperation(value = "解绑门店与员工", notes = "解绑门店与员工")
    @GetMapping(value = "/unbinding")
    public Result<?> unbinding(@RequestParam(name = "storeId") String storeId, @RequestParam(name = "employeePhone") String employeePhone) {
        storeEmployeeService.unbinding(storeId, employeePhone);
        return Result.OK("操作成功");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "门店员工表-通过id删除")
    @ApiOperation(value = "门店员工表-通过id删除", notes = "门店员工表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        storeEmployeeService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "门店员工表-批量删除")
    @ApiOperation(value = "门店员工表-批量删除", notes = "门店员工表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.storeEmployeeService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "门店员工表-通过id查询")
    @ApiOperation(value = "门店员工表-通过id查询", notes = "门店员工表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        StoreEmployeeEntity storeEmployee = storeEmployeeService.getById(id);
        if (storeEmployee == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(storeEmployee);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param storeEmployee
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StoreEmployeeEntity storeEmployee) {
        return super.exportXls(request, storeEmployee, StoreEmployeeEntity.class, "门店员工表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, StoreEmployeeEntity.class);
    }

}
